//
//  TTOAutoViewController.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/21.
//  Copyright © 2016年 陶路路. All rights reserved.
//


#import "TTOAutoViewController.h"
#import "TTProgressView.h"
#import "TTWelcomeModel.h"
@interface TTOAutoViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView * webView;

@property (nonatomic,strong) TTProgressView * progress;

@end

@implementation TTOAutoViewController

-(void)loadView{
    
    [super loadView];
    
    self.webView = [[UIWebView alloc]init];
    self.webView.delegate = self;
    self.view = self.webView;
    
    NSString * urlStringl = @"https://api.weibo.com/oauth2/authorize?client_id=2110075591&redirect_uri=http://www.baidu.com";
    NSURL * url = [NSURL URLWithString:urlStringl];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"微博登录";
    self.progress = [[TTProgressView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 2)];
    [self.navigationController.view insertSubview:self.progress belowSubview:self.navigationController.navigationBar];
    [self setUpUI];
}

-(void)setUpUI{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(didCancelButton)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"填充" style:UIBarButtonItemStylePlain target:self action:@selector(didFillButton)];
    
}


-(void)didCancelButton{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)didFillButton{

    NSString * jsString = [NSString stringWithFormat:@"document.getElementById('userId').value='%@',document.getElementById('passwd').value='%@'",wbName,wbPasswd];
    
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    
}

// webView代理方法

-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    [self.progress starAnimation];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self.progress endAnimation];
    [self.progress removeFromSuperview];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    /*
     获取请求的url字符串
     let urlString = request.URL?.absoluteString
     //        获取url前缀
     if let u = urlString where u.hasPrefix(APPREDIRECT_URI){
     //        获取请求参数
     let query = request.URL?.query
     
     if let q = query{
     //        分割字符串 获取参数
     
     let code = q.substringFromIndex("code=".endIndex)

     */
    
    NSString * urlString = request.URL.absoluteString;
    
    if ([urlString hasPrefix:APPREDIRECT_URI]) {
        
        NSString * query = request.URL.query;

        NSRange range = NSMakeRange(5, 32);
  
        NSString * code = [query substringWithRange:range];
        
        [[TTWelcomeModel sharedManager] getUserAccount:^(BOOL isSucc) {
            
            if (!isSucc) {
                
                return ;
            }
            
            [self dismissViewControllerAnimated:NO completion:^{
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WelcomeViewController" object:nil];
            }];
            
            
        } code:code];
        
        return NO;
    }
    
    
    return  YES;
}
-(void)dealloc{
    
    NSLog(@"销毁");
}
@end
